/**
 * @author monirul
 */

var RoomHistoryView =  new Class.create({
	
	initialize: function(){
	
	},
	
	init:function(o){
		if(o['componentId'] != null) this._componentId = o['componentId'];
		if(o['model'] != null) this._model = o['model'];
		if(o['controller'] != null) this._controller = o['controller'];
		if(o['elements'] != null){
			this._elements = o['elements'];
			this._targetLocation = this._elements['targetLocation'];
		}	
		 var _this = this;
		// attach model listeners
	    this._model.roomHistoryUpdated.attach(function () {
	        _this.rebuildList();
	    });
//	    this._model.displayRoomHistory.attach(function () {
//	        _this.showHistoryWindow();
//	    });
	   
	},
	show : function () {
        this.rebuildList();
	},
	
	
	rebuildList: function() {

		var myPanel =  new YAHOO.widget.Panel("historyPanel", { width:"720px", visible:true, constraintoviewport:true,underlay: "matte" ,fixedcenter: true } );
		myPanel.setHeader("History");
		//window.open('/historyWindow.html','History','width=300,height=300,scrollbars=yes');
		this.roomsHistoryData = new Array();
		var roomHistory = this._model.getRoomHistory();
		for(var i=0;i<roomHistory.length; i++){
				this.detailLink = '<input type="button"  onclick="alert(\'deail history\');componentExecFunctionWithCall(\''+this._componentId+'\',\'displayGameHistory\',\''+roomHistory[i].historyId+'\');"  value="detail"/>';
				this.roomsHistoryData[i] = {
											'startTime':roomHistory[i].startTime ,
											'endTime':roomHistory[i].endTime,
											'gameStatus':roomHistory[i].status,
											'noOfUsers':roomHistory[i].noOfUsers,
											'detailHistory':this.detailLink 
											}
		}
//		
		this.columnDefs = [ 
				            {key:"startTime", label:"Start Time", sortable:true, resizeable:true,width:195}, 
				            {key:"endTime",label:"End Time", sortable:true, resizeable:true,width:153},
				            {key:"gameStatus",label:"Game Status", sortable:true, resizeable:true,width:153},
				            {key:"noOfUsers",label:" Total # of Users", sortable:true, resizeable:true,width:153},
				            {key:"detailHistory",label:" # ", sortable:true, resizeable:true,width:153}
				            //, 
				            //{key:"owner",lebel:"Room Owner", resizeable:true}
				        ]; 
		this.rowFormatter = function(elTr, oRecord) { 
//			if (oRecord.getData('roomName').length>0 ) { 
//				YAHOO.util.Dom.addClass(elTr, 'mark'); 
//			} 
			return true; 
		}; 

		var init = {'colDef':this.columnDefs,'title':"Room History",'targetPosition':this._targetLocation};
		var roomListTbl = ComponentManager.loadComponent({componentId:'roomHistory_table',
			jsClassName:'DataTableWidget',initData:init});
//		roomListTbl.setRowFormatter(this.rowFormatter);
		roomListTbl.drawTable(this.roomsHistoryData);
		myPanel.render();
	},
	
	displayGameHistory:function(gameId){
//		alert('displayGameHistory');
		var myPanel =  new YAHOO.widget.Panel("historyPanel", { width:"720px", visible:true, constraintoviewport:true,underlay: "matte" ,fixedcenter: true } );
		myPanel.setHeader("History");
		this.roomsHistoryData = new Array();
		var gameUserHistoryList = this._model.getRoomGameHistory(gameId);
		if(gameUserHistoryList == null) return;
		for(var i=0;i<gameUserHistoryList.length; i++){
//				this.detailLink = '<input type="button"  onclick="alert(\'deail history\');componentExecFunctionWithCall(\''+this._componentId+'\',\'displayGameHistory\',null);"  value="detail"/>';
				var userStatus = "PLAYER"; 
				if(gameUserHistoryList[i].userStatus == 2 ){
					userStatus = "DEAD";
				}else if(gameUserHistoryList[i].userStatus == 6 ){
					userStatus = "WINNER";
				}
				var gameStatus = "INITIALIZED"; 
					if(gameUserHistoryList[i].userStatus == 1 ){
						gameStatus = "STARTED";
					}else if(gameUserHistoryList[i].userStatus == 3 ){
						gameStatus = "GAME OVER";
					}
					
				this.roomsHistoryData[i] = {
											'userName':gameUserHistoryList[i].userName ,
											'userStatus':userStatus,
											'gameStatus':gameStatus,
											'role':gameUserHistoryList[i].roleName,
											'character':gameUserHistoryList[i].characterName
											}
		}
//		
		this.columnDefs = [ 
				            {key:"userName", label:"User name", sortable:true, resizeable:true,width:195}, 
				            {key:"userStatus",label:"User Status", sortable:true, resizeable:true,width:153},
				            {key:"gameStatus",label:"Game Status", sortable:true, resizeable:true,width:153},
				            {key:"role",label:" Role", sortable:true, resizeable:true,width:153},
				            {key:"character",label:"Character ", sortable:true, resizeable:true,width:153}
				            
				            //, 
				            //{key:"owner",lebel:"Room Owner", resizeable:true}
				        ]; 
		this.rowFormatter = function(elTr, oRecord) { 
//			if (oRecord.getData('roomName').length>0 ) { 
//				YAHOO.util.Dom.addClass(elTr, 'mark'); 
//			} 
			return true; 
		}; 

		var init = {'colDef':this.columnDefs,'title':"Room History",'targetPosition':this._targetLocation};
		var roomListTbl = ComponentManager.loadComponent({componentId:'gameUserHistoryList_table',
			jsClassName:'DataTableWidget',initData:init});
//		roomListTbl.setRowFormatter(this.rowFormatter);
		roomListTbl.drawTable(this.roomsHistoryData);
		myPanel.render();
	}
	
	
	
});
